package cn.phecda.boot.job.xxljob.autoconfigure;

import cn.phecda.boot.job.xxljob.handler.HttpJobHandler;
import cn.phecda.boot.job.xxljob.handler.ShellJobHandler;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * <p>自动配置</p>
 *
 * @author wanpinwei
 */
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(XxlJobProperties.class)
public class JobXxlJobAutoConfiguration {
    @PostConstruct
    public void postConstruct() {
        log.info("[Alioth - Engine] |- [Xxl Job] Auto Configure.");
    }

    @Bean
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties properties) {
        XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
        executor.setAdminAddresses(properties.getAdmin().getAddresses());
        executor.setAppname(properties.getExecutor().getAppname());
        executor.setAddress(properties.getExecutor().getAddress());
        executor.setIp(properties.getExecutor().getIp());
        executor.setPort(properties.getExecutor().getPort());
        executor.setAccessToken(properties.getAccessToken());
        executor.setLogPath(properties.getExecutor().getLogpath());
        executor.setLogRetentionDays(properties.getExecutor().getLogretentiondays());
        log.trace("[Alioth - Engine] |- Bean [Xxl Job Spring Executor] Auto Configure.");
        return executor;
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpJobHandler httpJobHandler() {
        HttpJobHandler handler = new HttpJobHandler();
        log.trace("[Alioth - Engine] |- Bean [Http Job Handler] Auto Configure.");
        return handler;
    }

    @Bean
    @ConditionalOnMissingBean
    public ShellJobHandler shellJobHandler() {
        ShellJobHandler handler = new ShellJobHandler();
        log.trace("[Alioth - Engine] |- Bean [Shell Job Handler] Auto Configure.");
        return handler;
    }
}
